package gui.utilities;

import java.sql.Timestamp;
import java.util.Calendar;

public class DateDiffCalculator {

	public static String getDiffString(Timestamp time) {
		
		if (time == null)
			return "some time ago";
		
        long ms = (System.currentTimeMillis() - time.getTime());
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(ms);
        
        if (c.get(Calendar.YEAR) > 1970) {
        	if (c.get(Calendar.YEAR) == 1971) {
        		return (c.get(Calendar.YEAR) - 1970) + " year ago";
        	}
        	return (c.get(Calendar.YEAR) - 1970) + " years ago";
        }
        
        if (c.get(Calendar.MONTH) > 0) {
        	if (c.get(Calendar.MONTH) == 1) {
            	return c.get(Calendar.MONTH) + " month ago";        		
        	}
        	return c.get(Calendar.MONTH) + " months ago";
        }

        if (c.get(Calendar.WEEK_OF_MONTH) > 1) {
            if (c.get(Calendar.WEEK_OF_MONTH) == 2) {
            	return (c.get(Calendar.WEEK_OF_MONTH) - 1) + " week ago";            	
            }
        	return (c.get(Calendar.WEEK_OF_MONTH) - 1) + " weeks ago";
        }

        if (c.get(Calendar.DAY_OF_MONTH) > 1) {
        	if (c.get(Calendar.DAY_OF_MONTH) == 2) {
            	return (c.get(Calendar.DAY_OF_MONTH) - 1) + " day ago";        		
        	}
        	return (c.get(Calendar.DAY_OF_MONTH) - 1) + " days ago";
        }

        if (c.get(Calendar.HOUR) > 1) {
        	if (c.get(Calendar.HOUR) == 2) {
            	return (c.get(Calendar.HOUR) - 1) + " hour ago";        		
        	}
        	return (c.get(Calendar.HOUR) - 1) + " hours ago";
        }

        if (c.get(Calendar.MINUTE) > 0) {
            if (c.get(Calendar.MINUTE) == 1) {
            	return c.get(Calendar.MINUTE) + " minute ago";            	
            }
        	return c.get(Calendar.MINUTE) + " minutes ago";        	
        }

        if (c.get(Calendar.SECOND) == 1) {
            return c.get(Calendar.SECOND) + " second ago";        	
        }
        return c.get(Calendar.SECOND) + " seconds ago";

	}
	
}
